package com.hxkj.client.service.taglib.handle;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.*;
import com.hxkj.domain.po.*;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.zheng
 * @Description 数据处理封装
 * @Date 16:22 2018/9/14
 **/
public abstract class HandingDataBean {


    @Autowired
    private ColumnServiceClient columnServiceClient;

    @Autowired
    private ContentServiceClient contentServiceClient;

    @Autowired
    private TagsServiceClient tagsServiceClient;

    @Autowired
    private ArticleServiceClient articleServiceClient;


    private static final String WEBSITE_ADD = "WEBSITE_ADD";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;


    /**
     * @author Mr.zheng
     * @Description setContentMap 将文章扩展内容存入map封装进Article
     * @Date 17:49 2018/9/13
     **/
    public void setContentMap(Article article, List<ContentMapping> contentMappingList) throws Exception {
        Content content = new Content();
        content.setArticleId(article.getArticleId());
        content.setSiteId(article.getSiteId());
        ResultDataStruct<Content> result = contentServiceClient.getContent(content);
        if (result.getData() != null) {
            content = result.getData();
            String json = JSONObject.toJSONString(content);
            JSONObject jsonObject = JSONObject.parseObject(json);
            Map<String, String> map = new HashMap<>();
            for (ContentMapping Mapping : contentMappingList) {
                map.put(Mapping.getCustomName(), jsonObject.get(Mapping.getContentName()) == null ? "" : jsonObject.get(Mapping.getContentName()).toString());
            }
            article.setMap(map);
        }
    }


    /**
     * 为分类封装文章列表，先封装置顶文章，后封装其他文章
     **/
    public List<Article> getColumnASArticleList(int isStickCount, int count, Article article, List<Article> articleList, String articleIds) throws Exception {
        List<Article> articles;
        if (isStickCount > 0) {
            article.setCount(isStickCount);
            article.setIsStick(1);
            articleList = articleServiceClient.getArticleListByParam(article);
            if (articleList != null && articleList.size() > 0) {
                for (int i = 0; i < articleList.size(); i++) {
                    if (i == 0) {
                        articleIds = String.valueOf(articleList.get(i).getArticleId());
                    } else {
                        articleIds = articleIds + "," + String.valueOf(articleList.get(i).getArticleId());
                    }
                }
            }
            article.setArticleIds(articleIds);
        }

        if (articleList.size() < count) {
            article.setCount(count - articleList.size());
            article.setIsStick(null);
            articles = articleServiceClient.getArticleListByParam(article);
            //封装置顶文章后的其他文章
            articleList.addAll(articles);
        }
        return articleList;
    }


    /**
     * 获取当前分类下的文章列表
     **/
    public ResultPageStruct<Article> getArticleResultPageStruct(int pageNum, Column column) throws Exception {
        PageTo pageParam = new PageTo();
        pageParam.setPageNum(pageNum);
        pageParam.setPageSize(column == null ? 5 : (column.getPageSize() == null ? 5 : column.getPageSize()));

        Article article = new Article();
        article.setColumnId(column.getColumnId());
        article.setSiteId(column.getSiteId());
        article.setAuditType(0);
        article.setDelTag(0);

        ParameterDataStruct<Article> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(article);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<Article> resultPageStruct = articleServiceClient.getArticleList(parameterDataStruct);
        return resultPageStruct;
    }


    /***
     * Mr.zheng:修改分类选中状态
     * **/
    public OptionColumnTree modifyChecked(OptionColumnTree columnTree, Long columnId) {
        if (columnTree.getColumnId() == columnId) {
            //如果为当前分类则选中状态标为：1
            columnTree.setChecked(1);
        } else {
            //如果为当前分类则选中状态标为：0
            columnTree.setChecked(0);
            if (columnTree.getChildren() != null) {
                for (OptionColumnTree oct : columnTree.getChildren()) {
                    modifyChecked(oct, columnId);
                }
            }
        }
        return columnTree;
    }


    /***
     * Mr.zheng:获取顶级分类
     * **/
    public Column getColumnByParent(Long parentColumnId) throws Exception {
        Column column = new Column();
        column.setColumnId(parentColumnId);
        column = columnServiceClient.getColumn(column);
        if (column.getParentColumnId() != -1) {
            column = getColumnByParent(column.getParentColumnId());
        }
        return column;
    }


    /***
     * Mr.zheng:获取当前tags下的文章列表
     * ***/
    public ResultPageStruct<Article> getArticleResultPageStructByTags(int pageNum, Tags tags) throws Exception {
        PageTo pageParam = new PageTo();
        pageParam.setPageNum(pageNum);
        pageParam.setPageSize(10);

        Article article = new Article();
        article.setTagsId(tags.getTagsId());
        article.setSiteId(tags.getSiteId());
        article.setAuditType(0);
        article.setDelTag(0);

        ParameterDataStruct<Article> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(article);
        parameterDataStruct.setPageTo(pageParam);
        return articleServiceClient.getArticleListByTags(parameterDataStruct);
    }

    /**
     * 获取站点下的tags列表
     **/
    public ResultPageStruct<Tags> getTagsResultPageStruct(Tags tags) throws Exception {
        PageTo pageParam = new PageTo();
        pageParam.setPageNum(1);
        pageParam.setPageSize(tags.getCount());
        ParameterDataStruct<Tags> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(tags);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<Tags> resultPageStruct = tagsServiceClient.getHotTagsList(parameterDataStruct);
        return resultPageStruct;
    }

    /****
     * 获取url规则配置
     * 如果redis换成不存在;则从数据库中查询;如若存在;则从缓存中获取
     * **/
    protected WebsiteAddr getWebSiteAddr(Long siteId) throws Exception {
      /*  WebsiteAddr websiteAddr = (WebsiteAddr) redisTemplate.opsForValue().get(WEBSITE_ADD + siteId);
        if (websiteAddr != null) return websiteAddr;*/
        WebsiteAddr websiteAddrData = new WebsiteAddr();
        websiteAddrData.setDelTag(0L);
        websiteAddrData.setSiteId(siteId);
        WebsiteAddr websiteAddr = websiteAddrServiceClient.get(websiteAddrData).getData();
        return websiteAddr;
    }
}
